package cn.itcast.bos.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimesRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	
	@Override
	public void addFixedArea(FixedArea fixedArea) {
		
		fixedAreaRepository.save(fixedArea);
	}

	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification,
			Pageable pageable) {
		
		return fixedAreaRepository.findAll(specification, pageable);
	}

	@Autowired
	private CourierRepository courierRepository;
	@Autowired
	private TakeTimesRepository takeTimesRepository;
	
	@Override
	public void associationCourierToFixedArea(Integer courierId,
			Integer takeTimeId, FixedArea fixedArea) {
		
		/*
		 *  java.lang.ClassCastException: cn.itcast.bos.domain.base.Courier_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy
			org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:123)
			org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666)
			org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4399)
			org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:333)
			org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:254)
			org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134)
		 	
		 	这里使用get方法报这个异常
		 *
		 */
		//Courier courier = courierRepository.getOne(courierId);
		//TakeTime takeTime = takeTimesRepository.getOne(takeTimeId);
		
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimesRepository.findOne(takeTimeId);
		FixedArea fixedArea2 = fixedAreaRepository.findOne(fixedArea.getId());
		
		//这里应该是持久化对象之间建立关系
		//courier.getFixedAreas().add(fixedArea);
		fixedArea2.getCouriers().add(courier);
		courier.setTakeTime(takeTime);
	}

	
	
	
	
	
	
	
	
	

}
